<?php
include_once '../comic/WCPersistantObject.php';

/*
 * Copyright 2009 Dan Klco
 * This file is part of Daily WebComics.
 * 
 * Daily WebComics is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Daily WebComics is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Daily WebComics.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * This class acts as a link between two objects.  It holds the id of two objects
 * as well as the object types of each object.  It also has a relation_type attribute
 * that describes the relationship between the two objects.
 * 
 * @author daniel.klco
 * @version 20090227
 */
class WCRelation extends WCPersistantObject{
	//list of available attributes to make my life easier when assigning variables
	public static $ATTR_PARENT_ID='parent_id';
	public static $ATTR_PARENT_TYPE='parent_type';
	public static $ATTR_CHILD_ID='child_id';
	public static $ATTR_CHILD_TYPE='child_type';
	public static $ATTR_RELATION_TYPE='relation_type';
	
	//list of potential relationship types
	public static $PARTNER_RELATION = 'Partner relationship';
	public static $AUTHOR_COMIC_RELATION = 'Author-comic relationship';
	public static $COMIC_STRIP_COMIC_RELATION = 'Comic Strip-Comic relationship';
	public static $COMIC_STRIP_CONFIG_RELATION = 'Comic Strip-Config relationship';
	public static $COMIC_IMAGE_RELATION = 'Comic-Image relationship';
	
	/**
	 * Returns true if the object can be saved to the database.  In this case we
	 * have to check if there is a relationship that already exists that has the same
	 * objects and is the same relation_type as this one and is not actally this 
	 * object.
	 * 
	 * @param true if the object is new eg. going to be inserted, false if it is going to be updated
	 * @return true if the object can be saved, false otherwise
	 */
	protected function canSave($new){
		$pi = $this->getAttribute('parent_id');
		$pt = $this->getAttribute('parent_type');
		$ci = $this->getAttribute('child_id');
		$ct = $this->getAttribute('child_type');
		$rt = $this->getAttribute('relation_type');
		$rel = $this->retrieve($this->db,$pi,$pt,$ci,$ct,$rt);
		if($rel !== false){
			if(!$new && $this->getId() == $rel){
				return true;
			}
			return false;
		}else{
			return true;
		}
	}
	
	/**
	 * Creates a new WCRelation Object.  If a valid id is specified it will load the object from the database.
	 * If an id is not specified the object will be set 'blank' with no attributes set.
	 * 
	 * @param $db
	 * @param $id
	 * @return none
	 */
	public function __construct($db,$id=NULL){
		parent::__construct('wc_relation',$db,$id);
	}
	
	/**
	 * Creates the table in the for this object.  Passing in the delete flag as true will cause the function to
	 * delete the table if it already exists.  If this flag is false or omitted the function will throw an error if the 
	 * table already exists.
	 * 
	 * The columns for WCRelation are:
	 * 	id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
	 * 	relation_type VARCHAR(255)
	 * 	parent_id INT
	 * 	parent_type VARCHAR(50)
	 * 	child_id INT
	 * 	child_type VARCHAR(50)
	 * 
	 * @param $delete if true the table will be deleted if it already exists
	 * @return none
	 */
	public function createTable($delete=false){
		parent::createTable($delete);
		$this->db->executeQuery('CREATE TABLE '.$this->table.' (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,relation_type VARCHAR(255),parent_id INT,parent_type VARCHAR(50),child_id INT,child_type VARCHAR(50))'); 
	}
	
	/**
	 * Returns all of the accepted attributes for the object.  These attributes will be saved to the database
	 * when the object is saved.  The key is the name of the attribute the value is whether or not it requires
	 * quotes.
	 * 
	 * The Attributes for WCRelation are:
	 * 	relation_type
	 * 	parent_id
	 * 	parent_type
	 * 	child_id
	 * 	child_type
	 * 
	 * @return An array of the attributes for the object.
	 */
	public function objectAttributes(){
		return array('relation_type'=>true,'parent_id'=>false,'parent_type'=>true,'child_id'=>false,'child_type'=>true);
	}
	
	/**
	 * Returns all of the required attributes for the object.  If these attributes are not set, the object
	 * will throw an exception while attempting to save.
	 * 
	 * The required attributes for WCRelation are:
	 * 	parent_id
	 * 	parent_type
	 * 	child_id
	 * 	child_type
	 * @return An array of the required attributes for the object.
	 */
	public function requiredAttributes(){
		return array('parent_id','parent_type','child_id','child_type');
	}

	/**
	 * Loads a relationship that links both objects provided and is of the specified
	 * type or false if none found.
	 * 
	 * The required attributes for WCRelation are:
	 * 	parent_id
	 * 	parent_type
	 * 	child_id
	 * 	child_type
	 * @return An array of the required attributes for the object.
	 */
	public static function retrieve($db,$id1,$o1type,$id2,$o2type,$type){
		$data = array($id1,$o1type,$id2,$o2type,$id2,$o2type,$id1,$o1type,$type);
		$query = $db->prepare('SELECT id FROM wc_relation WHERE ((parent_id=? AND parent_type=\'?\' AND child_id=? AND child_type=\'?\') OR (parent_id=? AND parent_type=\'?\' AND child_id=? AND child_type=\'?\')) AND relation_type=\'>\'',$data);
		$result = $db->executeQuery($query);
		if(mysql_num_rows($result) != 1){
			return false;
		}
		return mysql_result($result,0,'id');
	}
}
?>